// pages/sign/up.js
const app = getApp();
Page({

  /**
   * 页面的初始数据
   */
  data: {
    array: [],
    index: 0,
    writeInfo: {}
  },

  changeZhuanYe: function (event){
    var nowMajor = this.data.array[event.detail.value];
    this.setData({
      index: event.detail.value
    });
  },

  onSubmit: function (event){
    var _that = this;
    var _data = event.detail.value;
    if(!_data.username){
      wx.showToast({
        title: '请填写姓名',
        icon:'none'
      });
      return false;
    }
    if(!_data.phone){
      wx.showToast({
        title: '请填写手机号码',
        icon:'none'
      });
      return false;
    }
    var reg1 = /^1[3456789]\d{9}$/;
    console.log(reg1, _data.phone);
    if(!reg1.test(_data.phone)){

      wx.showToast({
        title: '手机号码格式错误!',
        icon:'none'
      });
      return false;
    }

    wx.showToast({
      title: '正在提交信息...',
      icon:'none',
      duration: 200000
    });

    wx.request({
      url: app.globalData.requestUrl + 'api/sign/up',
      method: 'POST',
      data: {
        user_token: app.globalData.userToken,
        user_name: _data.username,
        phone: _data.phone,
        major: _that.data.array[_data.major].id
      },success: function (res){
        wx.hideToast();
        // 执行成功
        var data = res.data;
        if(data.status == 1){

          wx.showToast({
            title: '资料已经提交,请等待审核...',
            icon: 'none'
          });

          _that.setData({
            writeInfo: data.data.write_info
          })

        }else{
          wx.showToast({
            title: data.msg,
            icon: 'none'
          });
        }

      },fail: function (){

        wx.showToast({
          title: '系统错误请稍后重试',
          icon: 'none'
        });
      }
    });
    return false;
  },


  getUserInfo: function (){

      var _that = this;
      let _isChecked = false;

      var userToken = wx.getStorageSync('user_token');

      wx.request({
        url: app.globalData.requestUrl + 'api/get/userinfo',
        method: 'POST',
        data: {
          user_token: userToken
        },
        success: function (res) {
          if (res.data.status != 1) {
            wx.showToast({
              title: res.data.msg
            })
          } else {
            var resInfo = res.data.data.info;

            var _userInfo = null,
                _isChecked = false;
            if (resInfo) {
              _userInfo = resInfo;
              if (_userInfo.status == 1) {
                _isChecked = true;
              }
            }

            wx.setStorageSync('user_info', _userInfo);

            wx.setStorageSync('user_is_checked', _isChecked);

            app.globalData.isChecked = _isChecked;
            app.globalData.userInfo = _userInfo;

            if (_isChecked) {
              wx.switchTab({
                'url': '/pages/index/index'
              });
            }
          }
        }
      })
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {

    let _that = this;
    app.getToken(1).then(function (){
      _that.loadData();
    });
  },

  loadData: function (){
    var _that = this;

    //this.getUserInfo();

    wx.request({
      url: app.globalData.requestUrl + 'api/sign_up_data',
      method: 'POST',
      data: {
        user_token: app.globalData.userToken
      },
      success: function(res){
        var data = res.data;
        wx.hideToast();
        if(data.status != 1){
          wx.showToast({
            icon: 'none',
            title: data.msg
          })
        }else{
          if(data.data.is_can_write == 1){
            // 能继续填写资料
            _that.setData({
              array: data.data.major
            });
          }else{
            _that.setData({
              writeInfo: data.data.write_info
            })
          }
        }
      },
      fail: function (res){
        wx.showToast({
          icon: 'none',
          title: '系统错误请重试'
        })
      }
    })

  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {
  
  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
  
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {
  
  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {
  
  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {
    this.onLoad();
    wx.hideNavigationBarLoading() //完成停止加载
    wx.stopPullDownRefresh() //停止下拉刷新
  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {
  
  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {
  
  }
})